
class Animal{
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void eat(){
        System.out.println(name + "正在吃");
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Dog extends Animal{
    public Dog(String name , int age) {
        super(name , age);
    }
    public void wangWang(){
        System.out.println(name + "正在旺旺");
    }


    public void eat() {
        System.out.println(name + "吃狗粮");
    }
}

class Bird extends Animal{
    public Bird(String name, int age) {
        super(name, age);
    }
    public void fly(){
        System.out.println(name + "正在飞");
    }
}
public class Test1 {
    public static void main(String[] args) {
        Animal animal1 = new Bird("小鸟" , 10);
        //animal1.fly(); 错误的，不能调用子类中特有的方法
        //向下转型
        Bird bird = (Bird)animal1;
        bird.fly();
        ((Bird) animal1).fly();
    }
    /*public static void main(String[] args) {
        *//*Dog dog = new Dog("小狗" , 18);
        Animal animal = dog;*//*
        //1.这是向上转型的第一种方式
        Animal animal1 = new Dog("小狗" , 10);//父类引用了子类对象
        //2.第二种方式,通过方法传参
        Dog dog = new Dog("小狗" , 18);
        func1(dog);
        //3.返回值
       Animal animal = func2();
        animal.eat();


    }
    public static void func1(Animal animal) {

    }
    public static Animal func2() {
        return new Dog("小狗" , 10);
    }*/
    //继承为了达到代码的复用，对共性的抽取
    /*public static void main1(String[] args) {
        Dog dog = new Dog("小狗" , 18);
        dog.eat();
        dog.wangWang();
        System.out.println("=======");
        Animal animal =new Animal("animal" , 10);
        animal.eat();*/


    }

